<?php

class Events_Widgets extends WP_Widget {

	public function __construct() {
		parent::__construct(
	 		'events_widget', // Base ID
			'Events Widget', // Name
			array( 'description' => __( 'An Event  Widget', 'events' )) // Args
		);
	}

 	public function form( $instance ) {
			if ( isset($instance['title']))
    {
        $title = $instance['title'];
    }
    else
    {
        $title = __('Events','events');
    }
    ?>
<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>
<?php
	}

	public function update( $new_instance, $old_instance ) {
		// processes widget options to be saved
      $instance = array();
      $instance['title'] = strip_tags($new_instance['title']);
      
      return $instance;
	}

	public function widget( $args, $instance ) {
      extract( $args );
		$title = apply_filters( 'widget_title', $instance['title'] );

		

		
		$um_options = get_um_events_options();
    //get events posts
    $args = array('numberofposts' => $um_options->numberofposts,
                  'post_type' => 'um_events',
                  'order' => 'ASC',
                  'orderby' => 'meta_value',
                  'meta_key' => 'um_events_start_date'                 
        );
   
    
    $events = new WP_Query($args);
    
    $events = $events->posts;
    
     echo '<section id="coming-events" class="posts-list-widget">';
     
    if ( ! empty( $title ) )
       echo '<h3>' . $title . '</h3>';
    else
         echo '<h3> Events </h3>';
   
   
   // echo '<ul>';
    foreach ( $events as $event ){ $time = strtotime(get_post_meta($event->ID, 'um_events_start_date',true));
        echo '<article class="group">';
         
          if (!get_the_post_thumbnail($event->ID, 'thumbnail')):
          ?>
          <a href="#"><img src="<?php echo IMG , 'article.png'; ?>" alt="#" /></a>
        <?php 
          else:
             echo '<a href="' . get_permalink( $event->ID ). '">' . get_the_post_thumbnail($event->ID, 'thumbnail') . '</a>';   
          endif;
        echo '<div class="text">';
        if ( $time > current_time('timestamp') )
        {
            echo    '<time> '.  human_time_diff( $time, current_time('timestamp') ) . ' to go' .'</time>';
        }
        else
        {
            echo    '<time>'.  human_time_diff( $time, current_time('timestamp') ) . ' ago' .' </time>';
        }
       echo '<a href="'. get_permalink($event->ID) .'">';
        echo  '<h4 class="entry-title"> ' . $event->post_title . '</h4>';
       echo '</a>';         
        echo '<p class="entry-desc">';
                if ( $event->post_excerpt )
                    echo $event->post_excerpt;
                else
                {
                    $words = explode(' ', $event->post_content); //echo count($words);
                  
                    if ( count($words) > 9){
                        for ( $i = 0; $i < 9; $i++ )
                        {
                            echo $words[$i];
                            echo ( $i != 9 ) ? ' ' : '...';
                        }
                        
                    }
                    else{
                        echo $event->post_content;
                    }
                }
               echo '</p>';
         echo '</div>';
         echo '</article>';
    }
   // echo '</ul>';
   
    echo '</section>';
    
	}

}
add_action( 'widgets_init', create_function( '', 'register_widget( "Events_Widgets" );' ) );
//register_widget( 'Events_Widgets' );
